<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/10
 * Time: 10:10
 */

namespace app\admin\controller;


use app\admin\model\GoodsCategory as GoodsCategoryModel;


class GoodsCategoryController extends BaseController
{
    public function index()
    {
        $this->assign([
            'goodsCategory' => GoodsCategoryModel::all(),
        ]);
        return $this->fetch();
    }

    public function add()
    {
//        dump(GoodsCategoryModel::getGoodsOrder());die;
        if($this->request->isGet()){
            $this->assign('goodsCategory', GoodsCategoryModel::getGoodsOrder());
            return $this->fetch();
        }
        if($this->request->isPost()){
            $params = $this->request->param();
            if($this->request->file('file')){
                $uploadFile = uploadFile('file',100,100,'uploads/goods_category');
                $params['pic'] = $uploadFile['origin'];
                $params['sm_pic'] = $uploadFile['thumb'];
                $params['pic_origin'] = $uploadFile['tag'];
            }
            $parent_level = GoodsCategoryModel::field('level')->find($params['parent_id']);
            if(empty($parent_level)){
                $params['level'] = 1;
            }else{
                $params['level'] = $parent_level->level +1;
            }
            $model = new GoodsCategoryModel();
            if($model->allowField(true)->isUpdate(false)->save($params)){
                return ['error'=>0,'msg'=>'操作成功'];
            }else{
                return ['error'=>1,'msg'=>'操作失败'];
            }
        }
        return ['error'=>9,'msg'=>'非法操作'];
    }

    public function edi()
    {
        $params = $this->request->param();
        if($this->request->isGet()){
            $this->assign([
                'goodsCategory' =>GoodsCategoryModel::getGoodsOrder(),
                'goodsCate' => GoodsCategoryModel::find($params['id'])
            ]);
            return $this->fetch();
        }
        if($this->request->isPost()){
            if($this->request->file('file')){
                $uploadFile = uploadFile('file',100,100,'uploads/goods_category');
                $params['pic'] = $uploadFile['origin'];
                $params['sm_pic'] = $uploadFile['thumb'];
                $params['pic_origin'] = $uploadFile['tag'];
                $oldUploadFile = GoodsCategoryModel::field('pic,sm_pic,pic_origin')->find($params['id']);
                uploadDelete([$oldUploadFile->pic,$oldUploadFile->sm_pic],$oldUploadFile->pic_origin);
            }
            $parent_level = GoodsCategoryModel::field('level')->find($params['parent_id']);
            if(empty($parent_level)){
                $params['level'] = 1;
            }else{
                $params['level'] = $parent_level->level +1;
            }
            $model = new GoodsCategoryModel();
            if($model->allowField(true)->isUpdate(true)->save($params)){
                return ['error'=>0,'msg'=>'操作成功'];
            }else{
                return ['error'=>1,'msg'=>'操作失败'];
            }
        }
        return ['error'=>9,'msg'=>'非法操作'];
    }

    public function del()
    {
        $id = $this->request->param('id');
        $oldUploadFile = GoodsCategoryModel::field('pic,sm_pic,pic_origin')->find($id);
        uploadDelete([$oldUploadFile->pic,$oldUploadFile->sm_pic],$oldUploadFile->pic_origin);
        if(GoodsCategoryModel::where(['id'=>$id])->delete()){
            return ['error'=>0,'msg'=>'操作成功!'];
        }else{
            return ['error'=>1,'msg'=>'操作失败!'];
        }
    }
}